package org.sam.swing;

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.font.FontRenderContext;
import java.awt.font.GlyphVector;
import java.io.File;
import java.io.FileInputStream;

import javax.swing.JPanel;

/**
 * 根据字体绘制出我要的效果
 * @author sam
 *
 */
public class JPanelFont extends JPanel {
	
	private static final long serialVersionUID = 3050508796619481561L;
	
	/**
	 * 获取字体的函数
	 * @param fontFileName
	 * @param fontSize
	 * @return
	 */
	public Font getFont(String fontFileName, float fontSize) {
		try {
			File file = new File(fontFileName);
			FileInputStream aixing = new FileInputStream(file);
			Font dynamicFont = Font.createFont(Font.TRUETYPE_FONT, aixing);
			Font dynamicFontPt = dynamicFont.deriveFont(fontSize);
			aixing.close();
			return dynamicFontPt;
		} catch (Exception ex) {
			ex.printStackTrace();
		}

		return null;
	}
	
	/**
	 * 当前操作的字体
	 */
	private Font font;
	
	/**
	 * 
	 */
	private GlyphVector verctor;
	
	/**
	 * 初始化构造函数
	 */
	public JPanelFont(){
		font = this.getFont("C:/symbol.ttf", 11.0f);
		verctor = font.createGlyphVector(new FontRenderContext(null, true, true), "test");
	}

	@Override
	public void paint(Graphics g) {
		// TODO Auto-generated method stub
		super.paint(g);
	     Graphics2D g2 = (Graphics2D)g;
	     FontRenderContext frc = new FontRenderContext(null, true, true);
	     Font a = new Font("宋体" , Font.PLAIN , 11);
	     Font f = a.deriveFont(Font.PLAIN, 40);
	     System.out.println("font = " +f.getFontName());
	     GlyphVector gv = f.createGlyphVector(frc, "testtesdfdsss");
	     g.setFont(f);
	     g.setColor(Color.white);
	     g.fillRect(0,0,400,400);
	     g.setColor(Color.black);
	     g2.drawGlyphVector(gv, 5,200);
	     g2.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING,
	                         RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
	     g2.setRenderingHint(RenderingHints.KEY_FRACTIONALMETRICS,
	                         RenderingHints.VALUE_FRACTIONALMETRICS_ON);
	     g2.drawString("test", 5, 250);
	}
	
	

}
